package com.liangzhong.modules.techBar.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.TechBarEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.BuildingType;
	import com.liangzhong.proxyconstant.techBar.TechBarConst;
	import com.lz.football.module.techBar.ITechBarView;
	
	import flash.display.DisplayObject;
	
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *科技馆 
	 * @author lake
	 * 
	 */	
	public class TechBarMediator extends ModuleMediator
	{
		public static const MediatorName :String = "TechBarMediator";
		
		public function TechBarMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		/**
		 *获取视图 
		 * @return 
		 * 
		 */		
		public function get view():ITechBarView
		{
			return viewComponent as ITechBarView;
		}
		/**
		 * 初始化
		 * 
		 */		
		override public function initMediator():void
		{
			addEventListeners();
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			(view as DisplayObject).addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).addEventListener(ModuleEvent.DROP_TIME,dropTimeHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.GRADE_UP,gradeUpHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.EQUIP_COMBINE,equipCombineHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.MATERIAL_COMBINE,materialCombineHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.PLAYER_COMBINE,playerCombineHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.CLOTH_COMBINE,clothCombineHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.PLAYER_REFRESH,playerRefreshHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.ROLE_CHANGE,roleChangeHandler);
			(view as DisplayObject).addEventListener(TechBarEvent.ROLE_REFRESH,roleRefreshHandler);
		}
		/**
		 * 删除事件
		 * 
		 */		
		override public function removeEventListeners():void
		{
			(view as DisplayObject).removeEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			(view as DisplayObject).removeEventListener(ModuleEvent.DROP_TIME,dropTimeHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.GRADE_UP,gradeUpHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.EQUIP_COMBINE,equipCombineHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.MATERIAL_COMBINE,materialCombineHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.PLAYER_COMBINE,playerCombineHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.CLOTH_COMBINE,clothCombineHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.PLAYER_REFRESH,playerRefreshHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.ROLE_CHANGE,roleChangeHandler);
			(view as DisplayObject).removeEventListener(TechBarEvent.ROLE_REFRESH,roleRefreshHandler);
		}
		/**
		 * 删除模块
		 * @param event
		 * 
		 */
		private function onRemoveModuleHandler(e:ModuleEvent):void
		{
			removeMediator();
		}
		/**
		 *升级
		 * @param e
		 * 
		 */		
		private function gradeUpHandler(e :ModuleEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_GRADE_UP);
		}
		/**
		 *去除冷却时间 
		 * @param e
		 * 
		 */		
		private function dropTimeHandler(e :ModuleEvent):void
		{
			this.sendNotification(ApplicationConstants.Public_Drop_Time,e.param);
		}
		/**
		 *装备合成 
		 * @param e
		 * 
		 */		
		private function equipCombineHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_EQUIPCOMBINE,e.param);
		}
		/**
		 *材料合成 
		 * @param e
		 * 
		 */		
		private function materialCombineHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_MATERIALCOMBINE,e.param);
		}
		/**
		 *球员合成 
		 * @param e
		 * 
		 */		
		private function playerCombineHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_PLAYERCOMBINE,e.param);
		}
		/**
		 *球衣合成 
		 * @param e
		 * 
		 */		
		private function clothCombineHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_CLOTHCOMBINE,e.param);
		}
		/**
		 *球员洗练 
		 * @param e
		 * 
		 */		
		private function playerRefreshHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_PLAYERREFRESH,e.param);
		}
		/**
		 *球员转职 
		 * @param e
		 * 
		 */		
		private function roleChangeHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_ROLECHANGE,e.param);
		}
		/**
		 *角色强化 
		 * @param e
		 * 
		 */		
		private function roleRefreshHandler(e :TechBarEvent):void
		{
			this.sendNotification(ApplicationConstants.TECHBAR_ROLE_REFRESH,e.param);
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			super.removeMediator();
			facade.removeMediator(MediatorName);
		}
		/**
		 * 监听
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [TechBarConst.GRADE_UP_RESULT,
				NotificationConstants.COMMON_DROP_TIME,
				TechBarConst.EQUIP_COMBINE_RESULT,
				TechBarConst.MATERIAL_COMBINE_RESULT,
				TechBarConst.PLAYER_COMBINE_RESULT,
				TechBarConst.CLOTH_COMBINE_RESULT,
				TechBarConst.PLAYER_REFRESH_RESULT,
				TechBarConst.ROLE_CHANGE_RESULT,
				TechBarConst.ROLE_REFRESH_RESULT];
		}
		/**
		 * 捕获
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification :INotification):void
		{
			switch(notification.getName())
			{
				case TechBarConst.GRADE_UP_RESULT://升级
				{
					view.gradeUp();
					break;
				}
				case NotificationConstants.COMMON_DROP_TIME://冷却时间去除
				{
					if(notification.getBody() == BuildingType.TECH_BAR)
					{
						AppData.getInstance().techBarManager.techBarInfoVo.pretime = 0;
						view.dropTime();
					}
					break;
				}
				case TechBarConst.EQUIP_COMBINE_RESULT://装备合成
				{
					view.equipCombine(notification.getBody());
					break;
				}
				case TechBarConst.MATERIAL_COMBINE_RESULT://材料合成
				{
					view.materialCombine(notification.getBody());
					break;
				}
				case TechBarConst.PLAYER_COMBINE_RESULT://球员合成
				{
					view.playerCombine(notification.getBody());
					break;
				}
				case TechBarConst.CLOTH_COMBINE_RESULT://球衣合成
				{
					view.clothCombine(notification.getBody());
					break;
				}
				case TechBarConst.PLAYER_REFRESH_RESULT://球员洗练
				{
					view.playerRefresh(notification.getBody());
					break;
				}
				case TechBarConst.ROLE_CHANGE_RESULT://角色转职
				{
					view.roleChange(notification.getBody());
					break;
				}
				case TechBarConst.ROLE_REFRESH_RESULT://角色强化
				{
					view.roleRefresh(notification.getBody());
					break;
				}
			}
			
		}
		/**
		 * 注册
		 * 
		 */		
		override public function onRegister():void
		{
			//请求信息
			this.sendNotification(ApplicationConstants.TECHBAR_GET_INFO);
			//界面处理
			if(ResourceManager.instance.isContainResource(ResourceConst.TECHBAR_RESOURCE))
			{
				showUI();
				
			}else
			{
				loader = new QueueLoader();
				var config:XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).techbar[0];
				bytesTotal = int(config.@totalBytes);
				var releaseVersion :String;
				for each(var item:XML in config.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				
				loadRes();	
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			showUI();
			ResourceManager.instance.putResource(ResourceConst.TECHBAR_RESOURCE,resList);
			super.onLoadComplete(e);
		}
		/**
		 * 显示界面 
		 * 
		 */
		private function showUI():void
		{
			viewComponent = Reflection.createInstance("com.lz.football.module.techBar.view.TechBarView");
			view.initUI();
			PopUpManager.addPopUp(view as DisplayObject,GlobalDef.GameWindowContainer,GlobalDef.GameStage.stageWidth,GlobalDef.GameStage.stageWidth,false);
			addEventListeners();
			//设置缓存位图
			setCacheAsBitmap(true);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			view.destroyView();
			viewComponent = null;
		}
	}
}